package com.xiang.sql.service;

import java.util.List;

import jakarta.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiang.api.Util;
import com.xiang.sql.entity.User;
import com.xiang.sql.StaticSql;
import com.xiang.sql.entity.Goods;
import com.xiang.sql.entity.Stock;
import com.xiang.sql.mapper.GoodsMapper;

/**
 * 商品 服务实现类
 * 
 * @author mac-xiang
 */
@Service
public class GoodsService extends ServiceImpl<GoodsMapper, Goods> {
  @Resource
  GoodsMapper mapper;

  private QueryWrapper<Goods> readWrapper(JSONObject p, User per) {
    QueryWrapper<Goods> ret = Util.getQuery(p, Goods.class);
    String gid = p.getString("notgid");
    if (gid != null && gid.length() > 0) {
      ret.apply(String.format("id NOT IN(SELECT `goods` FROM `stock` WHERE `gid`='%s')", Util.safeSql(gid, null)));
    }
    if (per != null && !per.per("商品查询")) {
      // ret.eq("`create`", per.id);
    }
    return ret;
  }

  private List<Goods> getList(JSONObject p, User per) {
    return mapper.selectList(readWrapper(p, per));
  }

  private Page<Goods> getPage(Page<Goods> page, JSONObject p, User per) {
    return mapper.selectPage(page == null ? new Page<Goods>(1, 100) : page, readWrapper(p, per));
  }

  public Page<Goods> ylq() {
    return mapper.selectPage(new Page<Goods>(1, 10), new QueryWrapper<Goods>().orderByDesc("create_time"));
  }

  public Goods create(JSONObject p, User per) {
    if (per != null && per.per("商品创建")) {
      Goods entity = Util.getEntity(p, Goods.class);
      entity.createTime = null;
      entity.user = per.id;
      mapper.insert(entity);
      return entity;
    }
    throw new RuntimeException("没有权限");
  }

  public int update(JSONObject p, User per) {
    if (per != null && per.per("商品修改")) {
      Goods entity = Util.getEntity(p, Goods.class);
      QueryWrapper<Goods> qw = Util.getQuery(p, Goods.class);
      if (this.count(qw) > 10) {
        throw new RuntimeException("单次修改数量超过10条");
      }
      // entity.create = null;
      return mapper.update(entity, qw);
    }
    throw new RuntimeException("没有权限");
  }

  public Object read(JSONObject p, User per) {
    Page<Goods> page = Util.getPage(p, Goods.class);
    return page == null ? getList(p, per) : getPage(page, p, per);
  }

  public int delete(JSONObject p, User per) {
    if (per != null && per.per("商品删除")) {
      QueryWrapper<Goods> q = Util.getQuery(p, Goods.class);
      if (this.count(q) > 10) {
        throw new RuntimeException("删除数据的数量不能超过10条");
      }
      return mapper.delete(q);
    }
    throw new RuntimeException("没有权限");
  }

  public long count(JSONObject p, User per) {
    return this.count(readWrapper(p, per));
  }

  public boolean exists(String id) {
    return this.count(new QueryWrapper<Goods>().eq("id", id)) > 0;
  }

  public int getMoney(String id) {
    Goods goods = this.getById(id);
    if (goods != null) {
      return goods.money;
    }
    return 0;
  }

  public int getStock(JSONObject p, User per) {
    Stock stock = StaticSql.mapper.stock
        .selectOne(new QueryWrapper<Stock>().eq("gid", p.getString("gid")).eq("goods", p.getString("id")));
    if (stock != null) {
      return stock.sum;
    }
    return 0;
  }
}
